package doublelinkedlist;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-12
 * Time: 23:25
 */
public class MyLinkList implements  IList{


   static class ListNode{
       public int val;
       public ListNode prev;
       public ListNode next;

       public ListNode(int val) {
           this.val = val;
       }
   }

   public ListNode head;//头节点
   public ListNode last;//尾节点

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if (head == null) {
            head = last = node;
        }else {
            //所有的插入优先绑定后面
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if (head == null) {
            head = last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if (index > len || index < 0) {
            return;
        }

        if (index == len) {
            addLast(data);
        }

        if (index == 0) {
            addFirst(data);
        }

        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }


    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }



    @Override
    public void remove(int key) {
        ListNode cur = head;

        while (cur != null) {
            if (cur.val == key) {
            if (cur == head) {

                //当只有一个节点要删除时
                if (head == null) {
                    cur.next.prev = null;
                }
                head = head.next;
                //删除就走人
                return;
            }else {
                cur.prev.next = cur.next;//优化后，删除中间和尾巴的代码
                if (cur == last) {
                    last = cur.prev;
                }else {
                    cur.next.prev = cur.prev;
                }
                //删除就走人
                return;
            }
            }
            cur = cur.next;

        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;

        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {

                    //当只有一个节点要删除时，cur.next.prev = null会为空，所以加上if判断
                    if (head == null) {
                        cur.next.prev = null;
                    }
                    head = head.next;
                    //删除不能走人，接着删除后面。

                }else {
                    cur.prev.next = cur.next;//优化后，删除中间和尾巴的代码
                    if (cur == last) {
                        last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                    //删除不能走人，接着删除后面。
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int len = 0;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void display() {
       ListNode cur = head;
       while (cur != null) {
           System.out.print(cur.val + " ");
           cur = cur.next;
       }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
        //注意head和last节点在链表中还被引用着
        head = last = null;
    }
}
